package com.wswy.wzcx.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.che.libcommon.ui.BaseActivity;
import com.che.libcommon.utils.BothStr;
import com.che.libcommon.utils.RxBus;
import com.facebook.common.util.UriUtil;
import com.wswy.wzcx.R;
import com.wswy.wzcx.module.InternalMsgCode;
import com.wswy.wzcx.utils.FileUtils;
import com.wzcx.orclib.CameraActivity;

import java.io.File;


/**
 * Created by wangduheng26 on 2018/3/13.
 * wangduheng26@gmail.com
 */

public class PersonIDCardActivity extends BaseActivity implements View.OnClickListener {
    private ImageView ivFront;
    private ImageView ivBack;
    private TextView tvOk;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_id_card_upload);

        ivFront = findViewById(R.id.iv_zheng);
        ivBack = findViewById(R.id.iv_fan);
        tvOk = findViewById(R.id.tv_ok);

        ivFront.setOnClickListener(this);
        ivBack.setOnClickListener(this);
        tvOk.setOnClickListener(this);
        getFilePathBack().delete();
        getFilePathFront().delete();
    }

    public static final int REQUEST_CODE_ID_CARD_FRONT = 1001;
    public static final int REQUEST_CODE_ID_CARD_BACK = 1002;
    public static final String ID_CARD_FRONT_NAME = "id_card_front.jpg";
    public static final String ID_CARD_BACK_NAME = "id_card_back.jpg";

    @Override
    public void onClick(View v) {
        if (ivFront == v) {
            startTakeFrontPic();
        } else if (ivBack == v) {
            startTakeBackPic();
        } else {
            RxBus.getDefault().postWithCode(InternalMsgCode.CODE_TAKE_PIC_ID_CARD,new BothStr(getFilePathFront().getAbsolutePath(),getFilePathBack().getAbsolutePath()));
        }
    }

    private void startTakeBackPic() {
        Intent intent = new Intent(this, CameraActivity.class);
        intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                getFilePathBack().getAbsolutePath());
        intent.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
        startActivityForResult(intent, REQUEST_CODE_ID_CARD_BACK);
    }

    private void startTakeFrontPic() {
        Intent intent = new Intent(this, CameraActivity.class);
        intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                getFilePathFront().getAbsolutePath());
        intent.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
        startActivityForResult(intent, REQUEST_CODE_ID_CARD_FRONT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_ID_CARD_FRONT) {
                ivFront.setImageURI(UriUtil.parseUriOrNull("file://" + getFilePathFront().getAbsolutePath()));
            } else if (requestCode == REQUEST_CODE_ID_CARD_BACK) {
                ivBack.setImageURI(UriUtil.parseUriOrNull("file://" + getFilePathBack().getAbsolutePath()));
            }
            checkStatus();
        }
    }

    private void checkStatus() {
        File fileFront = getFilePathFront();
        File fileBack = getFilePathBack();
        if (fileFront.exists() && fileBack.exists()) {
            tvOk.setEnabled(true);
        } else {
            tvOk.setEnabled(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private File getFilePathFront() {
        return FileUtils.getSaveFile(this, ID_CARD_FRONT_NAME);
    }

    private File getFilePathBack() {
        return FileUtils.getSaveFile(this, ID_CARD_BACK_NAME);
    }
}
